package com.polidea.rxohosble2.internal.scan;


import com.polidea.rxohosble2.annotation.RestrictTo;
import com.polidea.rxohosble2.scan.ScanRecord;
import com.polidea.rxohosble2.annotation.Nullable;
import ohos.bluetooth.ble.BleScanResult;
import ohos.utils.PlainArray;
import ohos.utils.SequenceUuid;

import java.util.*;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class ScanRecordImplNativeWrapper implements ScanRecord {

    private final BleScanResult nativeScanRecord;

    public ScanRecordImplNativeWrapper(BleScanResult nativeScanRecord) {
        this.nativeScanRecord = nativeScanRecord;
    }

    @Override
    public int getAdvertiseFlags() {
        return nativeScanRecord.getAdvertiseFlag();
    }

    @Nullable
    @Override
    public List<SequenceUuid> getServiceUuids() {
        List<UUID> origin = nativeScanRecord.getServiceUuids();
        List<SequenceUuid> list = new ArrayList<>();
        if (origin != null) {
            for (UUID uuid : origin) {
                list.add(new SequenceUuid(uuid));
            }
        }
        return list;
    }

    @Override
    public PlainArray<byte[]> getManufacturerSpecificData() {
        Map<Integer, byte[]> origin = nativeScanRecord.getManufacturerData();
        PlainArray<byte[]> array = new PlainArray<>();
        if (origin != null) {
            for (Integer key : origin.keySet()) {
                array.put(key, origin.get(key));
            }
        }
        return array;
    }

    @Nullable
    @Override
    public byte[] getManufacturerSpecificData(int manufacturerId) {
        return nativeScanRecord.getManufacturerData().get(manufacturerId);
    }

    @Override
    public Map<SequenceUuid, byte[]> getServiceData() {
        Map<UUID, byte[]> origin = nativeScanRecord.getServiceData();
        Map<SequenceUuid, byte[]> map = new HashMap<>();
        if (origin != null) {
            for (UUID key : origin.keySet()) {
                map.put(new SequenceUuid(key), origin.get(key));
            }
        }
        return map;
    }

    @Nullable
    @Override
    public byte[] getServiceData(SequenceUuid serviceDataUuid) {
        return nativeScanRecord.getServiceData().get(serviceDataUuid.getUuid());
    }

    @Override
    public int getTxPowerLevel() {
        return nativeScanRecord.getRssi();
    }

    @Nullable
    @Override
    public String getDeviceName() {
        return nativeScanRecord.getPeripheralDevice().getDeviceName().orElse("");
    }

    @Override
    public byte[] getBytes() {
        return nativeScanRecord.getRawData();
    }
}
